package com.kate.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kate.mall.product.queue.Request;
import com.kate.mall.product.queue.RequestQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.BlockingQueue;

@Service
public class GoodsSpecificationServiceSyncImpl {

    /**
     * 保证同一个(id)规则的(读、写)请求,放到同一个阻塞队列中
     *
     * @param request
     */
    public void addRequest(Request request) throws InterruptedException {
        //1.获取规格id
        Integer goodsSpecificationId = request.getGoodsSpecificationId();
        //2.根据规格id，负载一个阻塞队列
        RequestQueue requestQueue = RequestQueue.queue();
        int index = goodsSpecificationId % requestQueue.getSize();
        BlockingQueue<Request> queue = requestQueue.index(index);
        //3.将请求加入到对应的阻塞队列
        queue.put(request);
    }

}